package com.example.makotoshoda.myapplication;

import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;


public class MainActivity extends ActionBarActivity {

    private TableLayout mTable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTable = (TableLayout) findViewById(R.id.tableLayout);
        // 追加
        ((Button)findViewById(R.id.btnAdd)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final EditText edit = new EditText(MainActivity.this);
                AlertDialog.Builder dlg = new AlertDialog.Builder(MainActivity.this);
                dlg.setView(edit);
                dlg.setTitle("New Name");
                dlg.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        MyDB db = new MyDB(MainActivity.this);
                        String name = edit.getText().toString();
                        db.saveUser(name, 0, 0, 0);
                        MainActivity.this.load();
                    }
                });
                dlg.show();
            }
        });
    }

    // 行追加
    private void addRow(int id, String name, int count) {
        MyDB db = new MyDB(this);
        MyRow row = MyRow.getRowByName(name);
        if (row != null) {
            return;
        }
        row = new MyRow(this, id, name, count);
        mTable.addView(row.getView());
    }

    // 読み込む
    private void load() {
        MyRow.clear();
        mTable.removeAllViews();

        MyDB db = new MyDB(this);
        // 読み込む
        List<ContentValues> users = db.users();
        for (ContentValues user : users) {
            int id = user.getAsInteger("id");
            String name = user.getAsString("name");
            int count = user.getAsInteger("counter");
            addRow(id, name, count);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        load();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onStart() {
        super.onStart();
        load();
    }

    @Override
    protected void onPause() {
        super.onPause();
        MyDB db = new MyDB(this);
        MyRow.saveAll(db);
    }
}
